package com.taptech.doufu.presenter.user

import android.app.Activity
import android.content.Intent
import com.google.gson.Gson
import com.taptech.doufu.bean.ShareBeansInfo
import com.taptech.doufu.constant.ShareType
import com.taptech.doufu.constant.SocialType
import com.taptech.doufu.constant.UserDetailCardType
import com.taptech.doufu.model.novel.bean.NovelAttentionBean
import com.taptech.doufu.model.user.*
import com.taptech.doufu.net.retrofit.api.BaseSubscriber
import com.taptech.doufu.presenter.BasePresenter
import com.taptech.doufu.ui.activity.UserDetailActivity
import com.taptech.doufu.util.GsonUtil

class UserDetailPresenter(activity: Activity, view: IUserDetailView,val intent:Intent) : BasePresenter<IUserDetailView>(activity, view) {
    val model = UserDetailModel(activity)
    var hasAttention = false  //是否关注了该作者
    val uid = intent.getIntExtra(UserDetailActivity.INTENT_KEY_UID,0)
    var nickname:String = ""
    var shareInfo:ShareBean = ShareBean()
//    var data:UserDetailBean = UserDetailBean()

    fun getUserDetail() {
        model.getUserDetail(uid,object :BaseSubscriber<UserDetailBean>(){
            override fun onNext(t: UserDetailBean) {
                super.onNext(t)
                if (mView != null) {
                    shareInfo = t.shareInfo
                    mView.getUserDetailResult(t)
                    hasAttention = t.following > 0
                    nickname = t.userInfo.nickname
                }
            }
        })
    }



    fun attentionUser() {
        if (hasAttention) {
            model.cancelAttentionUser(uid,object : BaseSubscriber<Boolean>(){

                override fun onNext(t: Boolean?) {
                    super.onNext(t)
                    if (t!=null && t) {
                        hasAttention = false
                        mView.attentionUser(true, success = true)
                    }else{
                        mView.attentionUser(true, success = false)
                    }
                }

                override fun onError(e: Throwable?) {
                    super.onError(e)
                    mView.attentionUser(true, success = false)
                }

            })
        }else{
            model.attentionUser(uid,object : BaseSubscriber<NovelAttentionBean>(){
                override fun onNext(t: NovelAttentionBean?) {
                    super.onNext(t)
                    if (t != null) {
                        hasAttention = true
                        mView.attentionUser(false, success = true)
                    }else{
                        mView.attentionUser(false, success = false)
                    }
                }

                override fun onError(e: Throwable?) {
                    super.onError(e)
                    mView.attentionUser(false, success = false)
                }
            })
        }
    }


    fun getShareBean(socialType:Int): ShareBeansInfo {
        val shareBean = ShareBeansInfo()
        shareBean.dataType = 1
        when(socialType){
            ShareType.pengyouquan->{
                shareBean.shartText = shareInfo.recommend_text
            }
            ShareType.QQSpace->{
                shareBean.shartText = shareInfo.recommend_text
            }
            ShareType.Weibo->{
                shareBean.shartText = shareInfo.recommend_text
            }
            else->{
                shareBean.shartText = shareInfo.description
            }
        }
        shareBean.imagUrl = shareInfo.cover
        shareBean.title = shareInfo.title
        shareBean.shareUrl = shareInfo.url
        return shareBean
    }

}